package ibm_batabase;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DbAccess
 */
public class DbAccess extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DbAccess() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		response.setContentType("text/html");
	    PrintWriter out = response.getWriter();
	    
	    try{
	         // Register JDBC driver
	         try {
	        	 
				Class.forName("com.mysql.jdbc.Driver");
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	         // Open a connection
	         conn = DriverManager.getConnection("jdbc:mysql://localhost/TEST","ahmed_ashry","");
	         // Execute SQL query
	         stmt = conn.createStatement();
	         String sql;
	         sql = "SELECT id, first, last, age FROM Employees";
	         //sql = "SELECT first FROM Employees";
	         rs = stmt.executeQuery(sql);
	         //String name_test_ajax=request.getParameter("q");
	         // Extract data from result set
	         while(rs.next()){
	            //Retrieve by column name
	            int id  = rs.getInt("id");
	            int age = rs.getInt("age");
	            String first = rs.getString("first");
	            String last = rs.getString("last");

	            //Display values
	            out.println("ID: " + id + "<br>");
	            out.println(", Age: " + age + "<br>");
	            out.println(", First: " + first + "<br>");
	            out.println(", Last: " + last + "<br>");
	        	 /*String first = rs.getString("first");
	        	 if(name_test_ajax.equals(first))
	        	 {
	        		 out.println(first);
	        		 break;
	        	 }*/
	         }
	         rs.close();
	         stmt.close();
	         conn.close();
	         
	         //response.sendRedirect("Test");
	         
	         //RequestDispatcher rd= request.getRequestDispatcher("Test");
	         //rd.include(request,response);
	         //rd.forward(request,response);
	         
	      }
	    catch(SQLException se){
	         //Handle errors for JDBC
	         se.printStackTrace();
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
